﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormTimChiTietHangXuat : Form
    {
        private DataContentDataContext db = new DataContentDataContext();
        private FormLapPhieuNhap frmLapPhieuNhap;
        public MATHANG MatHangadd = new MATHANG();

        public FormTimChiTietHangXuat()
        {
            InitializeComponent();
        }

        public FormTimChiTietHangXuat(FormLapPhieuNhap frm)
        {
            this.frmLapPhieuNhap = frm;
            InitializeComponent();

            //this.btnTimMH.Enabled = true;
            //this.btnChon.Enabled = false;
            //this.btnAddMH.Enabled = false;
            //this.btnXoaMH.Enabled = false;
            //this.btnHuyMH.Enabled = false;
            //this.btnThoat.Enabled = true;
            //this.btnThemMH.Enabled = false;
        }       

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;//khi click vao day thi tra ve dialog result la cancel
            this.Close();
        }

        private void btnHuyMH_Click(object sender, EventArgs e)
        {
            this.txtTenHang.ResetText();
            this.cmbLoaiHang.ResetText();
            this.cmbLoaiHang.Focus();
            this.dataGridViewDSLHThem.Rows.Clear();
        }

        private void btnTimMH_Click(object sender, EventArgs e)
        {
            var hangs = (from m in db.MATHANGs
                         where m.TenHang.StartsWith(this.txtTenHang.Text)
                         select m).ToList();
            this.mATHANGBindingSource.DataSource = hangs;
        }

        private void FormTimChiTietHangXuat_Load(object sender, EventArgs e)
        {
            this.dataGridViewDSLH.AutoGenerateColumns = false;
            var mathang = (from lh in db.MATHANGs
                           orderby lh.MaHang ascending
                           select lh).ToList();
            this.mATHANGBindingSource.DataSource = mathang;

            var loaihangs = (from m in db.LOAIHANGs
                             select m).ToList();
            this.bsLoaiHang.DataSource = loaihangs;
            this.cmbLoaiHang.DataSource = bsLoaiHang;
            // this.cmbLoaiHang.DisplayMember = "MaLoaiHang";
            this.cmbLoaiHang.DisplayMember = "TenLoaiHang";
            this.dataGridViewDSLHThem.DataSource = bsMatHangChon;           
        }

        private void bsLoaiHang_CurrentChanged(object sender, EventArgs e)
        {
            var loaihang = this.bsLoaiHang.Current as LOAIHANG;
            if (loaihang != null)
            {
                var hang = (from m in db.MATHANGs
                            where m.MaLoaiHang == loaihang.MaLoaiHang
                            select m).ToList();
                this.mATHANGBindingSource.DataSource = hang;
            }
        }

        private void btnAddMH_Click(object sender, EventArgs e)
        {
            if (this.frmLapPhieuNhap != null)
            {
                var tongtien = danhsachchitietchungtu.Sum(m => m.SoLuong * m.DonGia);
                this.frmLapPhieuNhap.cHITIETPHIEUNHAPBindingSource.DataSource = this.bsMatHangChon.DataSource;
                this.frmLapPhieuNhap.cHITIETPHIEUNHAPBindingSource.ResetBindings(true);
                this.frmLapPhieuNhap.numericUDTongtien.Value = tongtien;
                this.DialogResult = DialogResult.OK;
            }          
        }

        private List<ChitietNhapXuat> danhsachchitietchungtu = new List<ChitietNhapXuat>();       

        private void btnChon_Click(object sender, EventArgs e)
        {
            var hangs = this.mATHANGBindingSource.Current as MATHANG;
            //if (frmLapPhieuNhap != null)
            //{
                if (hangs != null)
                {
                    danhsachchitietchungtu = (List<ChitietNhapXuat>)this.bsMatHangChon.DataSource ?? new List<ChitietNhapXuat>();
                    var hangdangchon = (from m in danhsachchitietchungtu
                                        where m.MaHang == hangs.MaHang
                                        select m
                                        ).ToList();
                    //n^ếu đ~a chọn
                    if (hangdangchon.Count() > 0)
                    {
                        var hangchontmp = hangdangchon.First();
                        for (int i = 0; i < danhsachchitietchungtu.Count; i++)
                        {
                            if (danhsachchitietchungtu[i].MaHang == hangchontmp.MaHang)
                            {
                                danhsachchitietchungtu[i].SoLuong++;
                                break;
                            }
                        }
                    }
                    else
                    {
                        bsMatHangChon.ResetCurrentItem();
                        //this.bsMatHangChon.Add(new CHITIETCHUNGTU
                        //{
                        //    DonGiaXuat = hangs.GiaBan,
                        //    MaHang = hangs.MaHang,
                        //    SoLuongXuat = 1,
                        //    MATHANG = hangs
                        //});
                        danhsachchitietchungtu.Add(new ChitietNhapXuat
                        {
                            DonGia = hangs.GiaNhap ?? 0,
                            MaHang = hangs.MaHang,
                            SoLuong = 1,
                            TenHang = hangs.TenHang
                            //DonGiaXuat = hangs.GiaBan,
                            //MaHang = hangs.MaHang,
                            //SoLuongXuat = 1,
                            //MATHANG = hangs
                        });
                    }
                    this.bsMatHangChon.DataSource = danhsachchitietchungtu;
                    bsMatHangChon.ResetBindings(true);
                    // this.dataGridViewDSLHThem.DataSource = bsMatHangChon;
                }
            //}          
        }

        private void btnThemMH_Click(object sender, EventArgs e)
        {
            Form frmThemCTMH = new FormChiTietMatHang(this);
            var kq = frmThemCTMH.ShowDialog();

            if (kq == DialogResult.OK)
            {
                var loaihang = (from m in db.LOAIHANGs
                                select m).ToList();
                bsLoaiHang.DataSource = loaihang;
                bsLoaiHang.ResetBindings(true);

                MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.bsLoaiHang.MoveLast();
            }
        }

        private void btnXoaMH_Click(object sender, EventArgs e)
        {
            this.bsMatHangChon.RemoveCurrent();        
        }        
    }
}
